home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / C / Applications / RandomDot 1.1.0 / source / RandomDotPrint.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-19  |  3.3 KB  |  131 lines  |  [TEXT/KAHL]

  1. /* RandomDotPrint.c
  2.     by David Phillip Oster October 1994 oster@netcom.com
  3.     for:
  4.     Stuart Inglis singlis@waikato.ac.nz
  5.     Department of Computer Science
  6.     University of Waikato, Hamilton, New Zealand
  7.  */
  8. #include <Printing.h>
  9. #include "RandomDotMain.h"
  10. #include "RandomDotRes.h"
  11.  
  12. #include "Error.h"
  13. #include "RandomDotWin.h"
  14. #include "RandomDotPrint.h"
  15. #include "Utils.h"
  16.  
  17. static THPrint thePrin = NIL;
  18.  
  19. /* GetGlobalPrinRec - 
  20.     get a default print record from our resource file.
  21.  */
  22. static THPrint GetGlobalPrinRec(void){
  23.     if(NIL == thePrin){
  24.         thePrin = (THPrint) GetPreferencesHandle('Prec', 128);
  25.     }
  26.     if(NIL == thePrin){
  27.         thePrin = (THPrint) NewHandleClear(sizeof(TPrint));
  28.         PrintDefault(thePrin);
  29.     }
  30.     return thePrin;
  31. }
  32.  
  33. /* DoRandomDotPageSetup - 
  34.     fetch the inital print handle in the preferences file (if it exists)
  35.     run the page setup dialog. 
  36.     save the print handle in the preferences file (if it exists)
  37.  */
  38. OSErr DoRandomDotPageSetup(void){
  39.     OSErr    errCode;
  40.     THPrint    h;
  41.  
  42.     if(FreeMem() < 50000){
  43.         TellError(memFullErr);
  44.         return memFullErr;
  45.     }
  46.     PrOpen();    /* changes current res file to printer driver's */
  47.     if(noErr == (errCode = PrError())){
  48.         h = GetGlobalPrinRec();
  49.         PrValidate(h);
  50.         if(PrStlDialog(h)){
  51.             SavePreferencesResource((Handle) h, 'Prec', 128);
  52.         }
  53.         PrClose();
  54.     }
  55.     TellError(errCode);
  56.     return errCode;
  57. }
  58.  
  59. /* ScaleCenter - destR will be the same proportions as srcR, 
  60.     scaled by the ratio (hRes / srcRes), (vRes / srcRes)
  61.     centered on destR
  62.  */
  63. static void ScaleCenter(const Rect *srcR, Fixed hRes, Fixed vRes, const Rect *pageR, Rect *destR){
  64.     Point    where;
  65.     Fixed    srcRes;
  66.  
  67.     srcRes = 72L << 16;
  68.  
  69.     destR->left = destR->top = 0;
  70.     destR->right =  FixRound(FixMul(FixDiv(FixRatio(srcR->right - srcR->left, 1), srcRes), hRes));
  71.     destR->bottom = FixRound(FixMul(FixDiv(FixRatio(srcR->bottom - srcR->top, 1), srcRes), vRes));
  72.  
  73.     where.h = pageR->left + ( (pageR->right - pageR->left) - 
  74.                 (destR->right - destR->left) )/2;
  75.     where.v = pageR->top + ( (pageR->bottom - pageR->top) - 
  76.                 (destR->bottom - destR->top) )/2;
  77.     OffsetRect(destR, where.h - destR->left, where.v - destR->top);
  78. }
  79.  
  80. /* DoRandomDotPrint - 
  81.  */
  82. OSErr DoRandomDotPrint(void){
  83.     OSErr                errCode;
  84.     RandomDotWindowPtr    win;
  85.     Fixed                hRes, vRes;
  86.     Rect                paperR;
  87.     THPrint                h;
  88.     TPPrPort            prPort;
  89.     WindowPtr            save2;
  90.     Rect                frame;
  91.  
  92.     if(NIL != (win = (RandomDotWindowPtr) FrontWindow()) && userKind == ((WindowPeek) win)->windowKind){
  93.         SetPort((WindowPtr) win);
  94.     }else{
  95.         return noErr;
  96.     }
  97.     PrOpen();    /* changes current res file to printer driver's */
  98.     if(noErr == (errCode = PrError())){
  99.         h = GetGlobalPrinRec();
  100.         PrValidate(h);
  101.         if(PrJobDialog(h)){
  102.             hRes = FixRatio((**h).prInfo.iHRes, 1);    /* printer res in dpi */
  103.             vRes = FixRatio((**h).prInfo.iVRes, 1);    /* printer res in dpi */
  104.             paperR = (**h).rPaper;
  105.             prPort = NIL;
  106.             prPort = PrOpenDoc(h, NIL, NIL);  
  107.             if(noErr == (errCode = PrError())){
  108.                 PrOpenPage(prPort, NIL);
  109.                 if(noErr == (errCode = PrError())){
  110.                     GetPort(&save2);
  111.                     SetPort((GrafPtr) prPort);
  112.                     ScaleCenter(&win->frame, hRes, vRes, &paperR, &frame);
  113.                     RandomDotCopybitsWin(win, &frame);
  114.                     SetPort(save2);
  115.                 }
  116.                 PrClosePage(prPort);
  117.             }
  118.             if(NIL != prPort){
  119.                 PrCloseDoc(prPort);
  120.             }
  121.             if(noErr == errCode && bSpoolLoop == (**h).prJob.bJDocLoop){
  122.                 PrPicFile(h, NIL, NIL, NIL, NIL);
  123.             }
  124.         }
  125.         PrClose();
  126.     }
  127.     SetPort((WindowPtr) win);
  128.     TellError(errCode);
  129.     return errCode;
  130. }
  131.